local addonName, IWonew = ...
local currentOrders = nil
local RequestCrafterOrders_ing = false
-- local currentProfession = Enum.Profession.Enchanting -- Enum.Profession.Blacksmithing
local currentProfession = Enum.Profession.Blacksmithing -- Enum.Profession.Blacksmithing
local currentRunningOrder = nil
local currentRunningOrderState = 1 -- 1:未开始 2：已接单 3：已制作 4：已完成
local claimedChecked = false

function StartAutoCrafterOrdersAction1()
	
	if 9 > 2 then
		local tbl = {
			{name = "tom"},
			{name = "tom1"},
			{name = "tom2"},
			{name = "tom3"},
			{name = "tom4"},
		}
		print(IWonew.TableToString(tbl))
		table.remove(tbl, 3)
		print(IWonew.TableToString(tbl))
		print(#tbl)
		return
	end
	print(9999)
	local recipeNames = {}
	local testList = WOW_Blacksmithing_Recipe_List
	for i = 1, #testList do
		local recipeInfo = C_TradeSkillUI.GetRecipeInfo(testList[i].recipeID)
		if recipeInfo then
			table.insert(recipeNames, recipeInfo.name)
		end
		local craftingReagents = GetOrderCraftingReagents({spellID = testList[i].recipeID})
		
		local operationInfo = C_TradeSkillUI.GetCraftingOperationInfo(testList[i].recipeID, craftingReagents, IWonew.getRandomString(10), false)
		if operationInfo then
			print("operationInfo--" .. testList[i].recipeName, operationInfo.quality)
		end
	end
	print("#recipeNames", #recipeNames)
	print("#testList", #testList)
	print("recipeNames", IWonew.TableToString(recipeNames))
	-- local recipeID = 445366
  -- -- 配方所需的材料
  -- local craftingReagents = {
  --   {dataSlotIndex = 10, itemID = 219947, quantity = 5}, -- 2风暴之尘
  --   -- {dataSlotIndex = 2, itemID = 210937, quantity = 5}, -- 2星镔爪框
  -- }
	
  -- C_TradeSkillUI.CraftRecipe(recipeID, 1, craftingReagents, nil, nil, false)
	-- /dump C_Item.GetItemInfo(219947)
	local itemName = C_Item.GetItemInfo(213613)
	print("itemName", itemName)
	-- if itemInfo then
	-- 	print("itemInfo", itemInfo.itemName)
	-- end
	
end

function LoopOrder()
	local orderID = 269484527
	-- 接单
	C_CraftingOrders.ClaimOrder(orderID, Enum.Profession.Blacksmithing)
	local recipeID = 450258
	local craftingReagents = {
		{itemID = 222418, quantity = 1, dataSlotIndex = 2}
	}
	-- 制造
	C_TradeSkillUI.CraftRecipe(recipeID, 1, craftingReagents, nil, orderID, false)
	-- 完成订单
	C_CraftingOrders.FulfillOrder(orderID, "Success!!", Enum.Profession.Blacksmithing)
end

function CheckClaimedOrder()
	local order = C_CraftingOrders.GetClaimedOrder()
	if order then
		C_CraftingOrders.ReleaseOrder(order.orderID, currentProfession)
	end
	claimedChecked = true
end

function StartAutoCrafterOrdersAction()
	if currentOrders == nil then
		RequestCrafterOrders()
		return
	end
	if #currentOrders <= 0 then
		currentOrders = nil
		if currentProfession == Enum.Profession.Blacksmithing then
			IWonew.CreateTextFrame(80, "锻A", 240, 0)
			currentProfession = Enum.Profession.Enchanting
			return
		end
		if currentProfession == Enum.Profession.Enchanting then
			IWonew.CreateTextFrame(80, "附B", 320, 0)
		end
	end
	if currentRunningOrder == nil and currentOrders and #currentOrders > 0 then
		currentRunningOrder = currentOrders[1]
		currentRunningOrderState = 1
	end
	if currentRunningOrder and currentRunningOrderState == 1 then
		C_CraftingOrders.ClaimOrder(currentRunningOrder.orderID, currentProfession)
		currentRunningOrderState = 2
		print("接单")
		return
	end
	if currentRunningOrder and currentRunningOrderState == 2 then
		local craftingReagents = GetOrderCraftingReagents(currentRunningOrder)
		-- 制造
		print("craftingReagents", IWonew.TableToString(craftingReagents))
		C_TradeSkillUI.CraftRecipe(currentRunningOrder.spellID, 1, craftingReagents, nil, currentRunningOrder.orderID, false)
		currentRunningOrderState = 3
		print("制造")
		return
	end
	if currentRunningOrder and currentOrders and currentRunningOrderState == 3 then
		C_CraftingOrders.FulfillOrder(currentRunningOrder.orderID, "Success!!", currentProfession)
		currentRunningOrder = nil
		currentRunningOrderState = 1
		table.remove(currentOrders, 1)
		print("完成")
	end
end

function OldDebug()
	local orderID = 269484522
	-- local result = C_CraftingOrders.ClaimOrder(orderID, Enum.Profession.Blacksmithing)
	-- print("result", result)
	-- print("result1", IWonew.TableToString(result))
	local slotMods = C_TradeSkillUI.GetItemSlotModificationsForOrder(orderID)
	print("slotMods", IWonew.TableToString(slotMods))

	-- local recipeID = 450217
	-- local craftingReagents = {
	-- 	{itemID = 222418, quantity = 2, dataSlotIndex = 1}
	-- }
	-- C_TradeSkillUI.CraftRecipe(recipeID, 1, craftingReagents, nil, orderID, false)

	-- C_CraftingOrders.FulfillOrder(orderID, "Success!!", Enum.Profession.Blacksmithing)
	-- local skillLineID = 2872 -- 锻造skillLineID
	-- local configID = C_ProfSpecs.GetConfigIDForSkillLine(skillLineID)
	-- local pathID = 99267
	
	-- local description = C_ProfSpecs.GetDescriptionForPath(pathID)
	-- print("description:---", description)
	-- local specTabIDs = C_ProfSpecs.GetSpecTabIDsForSkillLine(skillLineID)
	-- print(IWonew.TableToString(specTabIDs))
	-- local tabInfo = C_ProfSpecs.GetTabInfo(966)
	-- print("tabInfo", IWonew.TableToString(tabInfo))
	-- local childIDs = C_ProfSpecs.GetChildrenForPath(tabInfo.rootNodeID)
	-- print("childIDs", IWonew.TableToString(childIDs))
	-- local testIds = {99267, 99266, 99265, 99264}
	-- for i = 1, #testIds do
	-- 	local nodeInfo = C_Traits.GetNodeInfo(configID, testIds[i])
	-- 	-- print("nodeInfo", IWonew.TableToString(nodeInfo))
	-- 	if nodeInfo then
	-- 		print("nodeInfo.activeRank", nodeInfo.activeRank)
	-- 	end

	-- end

	-- local configID = C_ProfSpecs.GetConfigIDForSkillLine(skillLineID)
	-- print(IWonew.TableToString(specTabIDs))
	-- local info = C_ProfSpecs.GetCurrencyInfoForSkillLine(skillLineID)
	-- print(IWonew.TableToString(info))
	-- local tabInfo = C_ProfSpecs.GetTabInfo(966)
	-- print("tabInfo", IWonew.TableToString(tabInfo))
	-- local pathID = 99267
	-- local currencyID = C_ProfSpecs.GetSpendCurrencyForPath(pathID)
	-- print("currencyID:---", currencyID)
	-- -- local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(currencyID)
	-- -- print("currencyInfo", IWonew.TableToString(currencyInfo))
	-- local description = C_ProfSpecs.GetDescriptionForPath(pathID)
	-- print("description:---", description)
	-- local perkInfos = C_ProfSpecs.GetPerksForPath(pathID)
	-- for i = 1, #perkInfos do
	-- 	local description = C_ProfSpecs.GetDescriptionForPerk(perkInfos[i].perkID)
	-- 	print("description:-----perkID", perkInfos[i].perkID, description)
	-- end

	
	-- print("perkInfos", IWonew.TableToString(perkInfos))
	-- local childIDs = C_ProfSpecs.GetChildrenForPath(pathID)
	
	-- -- print("childIDs", IWonew.TableToString(childIDs))
	-- for i = 1, #childIDs do
	-- 	local description = C_ProfSpecs.GetDescriptionForPath(childIDs[i])
	-- 	print("childIDs:---", childIDs[i], description)
	-- 	local currencyID = C_ProfSpecs.GetSpendCurrencyForPath(childIDs[i])
	-- 	print("currencyID:---", currencyID)
	-- 	if currencyID then
	-- 		local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(currencyID)
	-- 		print("currencyInfo", IWonew.TableToString(currencyInfo))
	-- 		print("quality----", currencyInfo.quality)
	-- 	end
	-- end
	print(6666)
	-- print("childIDs", IWonew.TableToString(childIDs))
	-- local currencyID = C_ProfSpecs.GetSpendCurrencyForPath(pathID)
	-- print("currencyID:---", currencyID)
	-- local currencyInfo = C_CurrencyInfo.GetCurrencyInfo(currencyID)
	-- print("currencyInfo", IWonew.TableToString(currencyInfo))
	-- local description = C_ProfSpecs.GetDescriptionForPath(pathID)
	-- print("description:---", description)

end

-- /dump RequestCrafterOrders()
-- 小兔罗西
function RequestCrafterOrders()
	if RequestCrafterOrders_ing then return end
	RequestCrafterOrders_ing = true
  local request = {
		orderType = Enum.CraftingOrderType.Npc,
		searchFavorites = false,
		initialNonPublicSearch = false,
		primarySort = {
			sortType = Enum.CraftingOrderSortType.ItemName,
			reversed = false,
		},
		secondarySort = {
			sortType = Enum.CraftingOrderSortType.MaxTip,
			reversed = false,
		},
		forCrafter = true,
		offset = 0,
		-- profession = Enum.Profession.Enchanting,
		profession = currentProfession,
		callback = C_FunctionContainers.CreateCallback(function(result, ...)
			print("res", result, ...) -- 0, 0, false, false, 0, true
			if result == Enum.CraftingOrderResult.Ok then
				local orders = C_CraftingOrders.GetCrafterOrders()
				-- IWonew.Log("orders---" .. IWonew.TableToString(orders))
				-- currentOrders = FilterCorrectOrders(IWonew.slice(orders, 4, 4))
				currentOrders = FilterCorrectOrders(orders)
				print("#currentOrders", #currentOrders)
				
				RequestCrafterOrders_ing = false
			end
		end),
	}
  C_CraftingOrders.RequestCrafterOrders(request)
end

function FilterCorrectOrders(orders)
	print("#orders", #orders)
	local correctOrders = {}
	for i = 1, #orders do
		if IsOrderCorrect(orders[i]) then
			table.insert(correctOrders, orders[i])
		end
	end
	return correctOrders
end

-- /myslot load 制造

function IsOrderCorrect(order)
	print("order", order.orderID, order.spellID)
	local recipeID = order.spellID
	local dd = {
		{
			orderState = 2, 
			orderType = 3, 
			npcOrderRewards = {
				{
					count = 1, 
					itemLink = "|cff1eff00|Hitem:225671::::::::80:102:::::::::|h[一叠五星点评]|h|r"
				}, 
				{
					count = 10, 
					itemLink = "|cff0070dd|Hitem:210814::::::::80:102:::::::::|h[匠人之敏]|h|r"
				}
			}, 
			npcCustomerCreatureID = 217091, 
			expirationTime = 1735124400,
			consortiumCut = 36114, 
			npcTreasureID = 128339,
			skillLineAbilityID = 50590,
			customerNotes = "", 
			crafterGuid = "Player-741-04F03AB4", 
			reagents = {
				{
					slotIndex = 1, 
					reagent = {quantity = 1, itemID = 213613, dataSlotIndex = 1}, 
					source = 0, 
					isBasicReagent = true
				}, 
				{
					slotIndex = 2, 
					reagent = {quantity = 1, itemID = 213612, dataSlotIndex = 2}, 
					source = 0, 
					isBasicReagent = true
				}, 
				{
					slotIndex = 3, 
					reagent = {quantity = 5, itemID = 219947, dataSlotIndex = 1}, 
					source = 0, 
					isBasicReagent = true
				}
			}, 
			isFulfillable = false, 
			customerName = "沃克格雷",
			orderID = 269932706, 
			crafterName = "强丶壮", 
			npcCraftingOrderSetID = 45, 
			reagentState = 0, 
			minQuality = 2, 
			itemID = 224315, 
			isRecraft = false, 
			claimEndTime = 0, 
			tipAmount = 722285, 
			spellID = 445319
		}
	}
	-- local fullRecipeList = IWonew.concat(WOW_Enchanting_Recipe_List, WOW_Blacksmithing_Recipe_List)
	-- local filterLocalRecipeList = IWonew.filterTable(WOW_Enchanting_Recipe_List, function(item)
	local filterLocalRecipeList = IWonew.filterTable(WOW_Full_Recipe_List, function(item)
		return item.recipeID == order.spellID
	end)
	-- print("filterLocalRecipeList", #filterLocalRecipeList)
	if #filterLocalRecipeList <= 0 then return false end

	local mustUseNpcRegentIDs = {}
	for i = 1, #filterLocalRecipeList[1].reagents do
		if filterLocalRecipeList[1].reagents[i].mustUseNpcRegent then
			table.insert(mustUseNpcRegentIDs, filterLocalRecipeList[1].reagents[i].itemID)
		end
	end
	-- print("mustUseNpcRegentIDs", IWonew.TableToString(mustUseNpcRegentIDs))
	local orderNpcReagentItemIDs = GetOrderNpcReagentItemIDs(order)
	local npcNotProvideRegents = IWonew.filterTable(mustUseNpcRegentIDs, function(itemID)
		return not IWonew.contains(orderNpcReagentItemIDs, itemID)
	end)
	if #npcNotProvideRegents > 0 then return false end
	-- print("npcNotProvideRegents", IWonew.TableToString(npcNotProvideRegents))
	-- print("order", IWonew.TableToString(order))
	-- print("order.spellID", order.spellID)
	-- print("order.orderID", order.orderID)
	
	-- print("recipeInfo", IWonew.TableToString(recipeInfo))

	local recipeInfo = C_TradeSkillUI.GetRecipeInfo(recipeID)
	if not recipeInfo then return false end
	if not recipeInfo.learned then return false end
	local craftingReagents = GetOrderCraftingReagents(order, true)
	local operationInfo = C_TradeSkillUI.GetCraftingOperationInfo(recipeID, craftingReagents, IWonew.getRandomString(10), false)
	-- C_TradeSkillUI.GetCraftingOperationInfoForOrder(recipeID, craftingReagents, orderID, applyConcentration)
	-- local operationInfo = C_TradeSkillUI.GetCraftingOperationInfoForOrder(recipeID, craftingReagents, order.orderID, false)
	if operationInfo == nil or order.minQuality > operationInfo.quality then return false end
	return true
end

function GetOrderNpcReagentItemIDs(order)
	local npcProvideRegents = {}
	for i = 1, #order.reagents do
		table.insert(npcProvideRegents, order.reagents[i].reagent.itemID)
	end
	return npcProvideRegents
end

function GetOrderCraftingReagents(order, ignoreNpcSlotItems)
	ignoreNpcSlotItems = ignoreNpcSlotItems or false
	local filterLocalRecipeList = IWonew.filterTable(WOW_Full_Recipe_List, function(item)
		return item.recipeID == order.spellID
	end)
	if #filterLocalRecipeList <= 0 then return {} end
	local localRecipeInfo = filterLocalRecipeList[1]
	local orderNpcReagentItemIDs = GetOrderNpcReagentItemIDs(order)
	local craftingReagents = {}
	for j = 1, #localRecipeInfo.reagents do
		
		if localRecipeInfo.reagents[j].needSlot and (ignoreNpcSlotItems or not IWonew.contains(orderNpcReagentItemIDs, localRecipeInfo.reagents[j].itemID)) then
			table.insert(craftingReagents, {
				quantity = localRecipeInfo.reagents[j].quantity,
				dataSlotIndex = localRecipeInfo.reagents[j].dataSlotIndex,
				itemID = localRecipeInfo.reagents[j].itemID
			})
		end
	end
	return craftingReagents
end

function CrafterOrders_Debug()
	-- isOrderGetting = false
	-- -- BlacksmithingOrders = orders
	-- -- local firstOrder = orders[1]
	-- -- C_CraftingOrders.ClaimOrder(firstOrder.orderID, Enum.Profession.Tailoring)
	-- -- print(IWonew.TableToString(firstOrder))
	-- -- local recipeInfo = C_TradeSkillUI.GetRecipeInfo(firstOrder.spellID)
	-- -- print("recipeInfo", IWonew.TableToString(recipeInfo))
	-- -- local itemIDs = C_TradeSkillUI.GetReagentRequirementItemIDs(firstOrder.spellID)
	-- -- print("itemIDs", IWonew.TableToString(itemIDs))

	-- -- local filtered = FilterOrder(orders, 450261)
	-- local filtered = FilterOrder(orders, 445366)
	-- if filtered then
	-- 	IWonew.Log("filtered" .. IWonew.TableToString(filtered))
	-- 	print(IWonew.TableToString(filtered))
	-- 	print("reagents", #filtered.reagents)
	-- 	if #filtered.reagents > 0 then
	-- 		for i = 1, #filtered.reagents do
	-- 			print("reagent", i, IWonew.TableToString(filtered.reagents[i]))
	-- 		end
	-- 	end

	-- 	local recipeInfo = C_TradeSkillUI.GetRecipeInfo(filtered.spellID)
	-- 	if recipeInfo then
	-- 		print("learned", recipeInfo.learned)
	-- 		print("recipeInfo", IWonew.TableToString(recipeInfo))
	-- 	end
	-- end
end

function FilterOrderByID(tbl, recipeID)
	local filtered = nil
	for i = 1, #tbl do
		if recipeID == tbl[i].spellID then
			filtered = tbl[i]
		end
	end
	return filtered
end

-- /dump C_CraftingOrders.ClaimOrder(249576413, 7)
-- /dump GetClaimedOrder()
function GetClaimedOrder()
  local order = C_CraftingOrders.GetClaimedOrder()
  print("GetClaimedOrder--order")
  print(IWonew.TableToString(order))
end



